package com.sdtele.tta.operation;

import java.io.File;

import com.sdtele.tta.dac.Dac;
import com.sdtele.tta.models.Doc;
import com.sdtele.tta.models.JsonRequest;
import com.sdtele.tta.models.JsonResponse;
import com.sdtele.tta.models.Task;
import com.sdtele.tta.models.User;


/**
 * 操作类实现：GetUser(json.operation=getUser)
 * 
 * @author XCream
 */
public class OperationDelDoc extends Operation {

	@Override
	public JsonResponse handle(JsonRequest jsonRequest) {
		JsonResponse jres = new JsonResponse();
		User curUser = (User) request.getSession().getAttribute("user");
		if (curUser != null) {
			long docId = jsonRequest.getJsonObject().get("docId").getAsLong();
			Doc doc = Dac.getInstance().getDoc(docId);

			Task task = Dac.getInstance().getTaskbyTaskid(doc.getTaskId());

			if (curUser.getId() == task.getOwnerId()) {

				if (doc != null) {
					String realPath = doc.getRealPath();
					File docFile = new File(realPath);
					if (docFile.exists()) {
						docFile.delete();
					
					}
				}
			} else {
				jres.setStatus(1);
				jres.setMessage("您没有权限删除此附件");
			}
		} else {
			jres.setStatus(1);
			jres.setMessage("用户未登录");
		}
		return jres;
	}
}
